package com.weixinli.web.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.weixinli.beans.Article;
import com.weixinli.beans.PageInfo;
import com.weixinli.biz.IArticleBiz;
import com.weixinli.utils.PageUtil;
import com.weixinli.utils.SessionAttributeName;
import com.weixinli.web.actions.model.JsonObject;

@Controller("articleAction")
public class ArticleAction extends ActionSupport implements SessionAware{
	
	private Article article;
	private PageInfo pageInfo;
	private PageUtil pageUtil;
	
	private String atype; //获取类型
	private String length; 
	private int page;
	private int rows;
	private Map<String,Object> session;
	
	
	@Resource(name="jsonObject")
	private JsonObject jsonObject;
	@Resource(name="iArticleBiz")
	private IArticleBiz iArticleBiz;
	
	private File file; //要上传的表单元素的name属性值，这个名字必须和input上传文件标签里的name相同
	private String fileContentType;//上传文件的类型
	private String fileFileName; //上传文件的名称
	private String savePath;//用来接受依赖注入的文件保存路径
	
	/**
	 * 获取单个文章
	 * @return
	 */
	public String getOneArticle(){
		Article at=iArticleBiz.getOneArticle(article);
		session.put(SessionAttributeName.ONEARTICLE, at);
		jsonObject=new JsonObject();
		if(at!=null){
			jsonObject.setResult(1);
		}else{
			jsonObject.setResult(0);
		}
		return "success";
	}
	
	
	/**
	 * 获取更多文章
	 * @return
	 */
	public String getMoreArticle(){
		List<Article> list=(List<Article>) session.get(SessionAttributeName.CURRENTARTICLE);
		List<Article> temp=new ArrayList<Article>();
		if(list.size()>Integer.valueOf(length)){
			for(int i=list.size()-Integer.valueOf(length)+1;i<list.size();i++){
				temp.add(list.get(i));
			}
		}
		int total=iArticleBiz.getTotalArticle();
		jsonObject=new JsonObject();
		jsonObject.setRows(temp);
		jsonObject.setTotal(total);
		return "success";
	}
	
	
	/**
	 * 删除文章
	 * @return
	 */
	public String delArticle(){
		int result=iArticleBiz.delArticle(article);
		jsonObject=new JsonObject();
		jsonObject.setResult(result);
		return "success";
	}
	
	/**
	 * 前台查询文章
	 * @return
	 */
	public String findAllArticle(){
		jsonObject=new JsonObject();
		int total=iArticleBiz.getTotalArticle();
		List<Article> list=iArticleBiz.findAll(atype);
		session.put(SessionAttributeName.CURRENTARTICLE, list); //把所有的文章放到session中
		jsonObject.setRows(list);
		return "success";
	}
	
	
	/**
	 * 后台分页查询文章
	 * @return
	 */
	public String findArticle(){
		jsonObject=new JsonObject();
		List<Article> list=iArticleBiz.findPage(page,rows);
		int total=iArticleBiz.getTotalArticle();
		jsonObject.setRows(list);
		jsonObject.setTotal(total);
		return "success";
	}
	
	/**
	 * 添加新文章
	 * @return
	 */
	public String addArticle(){
		
		jsonObject=new JsonObject();
		
		FileOutputStream fos=null;
		FileInputStream fis=null;
		
		try {
			fos=new FileOutputStream(getSavePath()+"\\"+getFileFileName());
			fis=new FileInputStream(file);
			
			byte[] buffer=new byte[1024];
			int len=0;
			
			while( (len=fis.read(buffer))>0){
				fos.write(buffer,0,len);
			}
			fos.flush();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return "error";
		} catch (IOException e) {
			e.printStackTrace();
			return "error";
		}finally{
			if(fos!=null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if(fis!=null){
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		}
		article.setAaddr(savePath+"/"+getFileFileName());
		int result=iArticleBiz.addArticle(article);
		if(result>0){
			jsonObject.setResult(result);
			
		}else{
			jsonObject.setResult(0);
			
		}
		return "success";
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = new Date().getTime()+ fileFileName;
	}

	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public JsonObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JsonObject jsonObject) {
		this.jsonObject = jsonObject;
	}

	public void setiArticleBiz(IArticleBiz iArticleBiz) {
		this.iArticleBiz = iArticleBiz;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	public String getAtype() {
		return atype;
	}

	public void setAtype(String atype) {
		this.atype = atype;
	}

	public String getLength() {
		return length;
	}

	public void setLength(String length) {
		this.length = length;
	}

	public void setSession(Map<String, Object> arg0) {
		this.session=arg0;
	}

}
